<?php
declare(strict_types=1);

namespace App\Controller\WebApi\v1;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Controller;
use App\Middleware\WebApiMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;
use Service\UserService\AddressService;
use Hyperf\Di\Annotation\Inject;

/**
 * 收获地址
 * @author huj
 * @Controller(prefix="/webapi/v1/address")
 * @Middleware(WebApiMiddleware::class)
 */
class AddressController extends AbstractController
{
    /**
     * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
     * @Inject()
     * @var AddressService
     */
    protected $addressService;

    /**
     * 收货地址列表
     * @RequestMapping(path="index", methods="get,post")
     */
    public function index(RequestInterface $request) 
    {
        $param        = $request->inputs(['page', 'limit', 'cid', 'key_word']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->addressList($param);
    }

    /**
     * 新增收货地址
     * @RequestMapping(path="add", methods="get,post")
     */
    public function add(RequestInterface $request)
    {
        $param        = $request->inputs(['true_name', 'idcard', 'mobile', 'province', 'city', 'area', 'address', 'is_default','cid','card_front','card_back']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->addAddress($param);
    }

    /**
     * 编辑收货地址
     * @RequestMapping(path="edit", methods="get,post")
     */
    public function edit(RequestInterface $request)
    {
        $param        = $request->inputs(['id', 'true_name', 'idcard', 'mobile', 'province', 'city', 'area', 'address', 'is_default','cid','card_front','card_back']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->editAddress($param);
    }

    /**
     * 删除收货地址
     * @RequestMapping(path="del", methods="get,post")
     */
    public function del(RequestInterface $request)
    {
        $param        = $request->inputs(['id']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->delAddress($param);
    }

    /**
     * 地址信息自动识别
     * @RequestMapping(path="getAddressResult", methods="get,post")
     */
    public function getAddressResult(RequestInterface $request)
    {
        $param        = $request->inputs(['address']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->getAddressResult($param);
    }

    /**
     * 地址信息自动识别
     * @RequestMapping(path="getDefaultAddress", methods="get,post")
     */
    public function getDefaultAddress(RequestInterface $request)
    {
        $param        = $request->inputs(['cid']);
        $param['mid'] = $request->getAttribute('mid');
        return $this->addressService->getDefaultAddress($param);
    }
}